package com.diyunapp.xilinapp.me;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.base.BaseActivity;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.utils.City;
import com.diyunapp.xilinapp.utils.MyAsyncTask;
import com.diyunapp.xilinapp.utils.UploadUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class iconsActivity extends BaseActivity implements
        View.OnClickListener, UploadUtil.OnUploadProcessListener {

    @Bind(R.id.button)
    Button button;
    @Bind(R.id.imageView4)
    ImageView imageView4;
    @Bind(R.id.imageView5)
    ImageView imageView5;

    private final static int PHOTO_WITH_CAMERA = 1;
    private final static int PHOTO_WITH_DATA = 2;
    private final static int CHANGE_PHONE_CODE = 3;
    private final static int CHANGE_EMAIL_CODE = 4;
    private final static int CHANGE_LOGIN_PASS_CODE = 5;
    private final static int CHANGE_PAY_PASS_CODE = 6;
    private Dialog setHeadPicture_Dialog;
    private TextView take_photo_tv;
    private TextView select_picture_tv;
    private TextView cancel_set_tv;
    private TextView select_female_tv;
    private TextView select_serect_tv;
    private TextView select_male_tv;
    private Uri myPhotoUri;
    private String myPhotoPath;
    private Bitmap myNewHeadPic;
    private static final long PICSIZELIMIT = 480000;

    //各类操作提示对话框
    private Dialog picturePrompt_Dialog;
    private TextView prompt_title_tv;
    private TextView prompt_info_tv;
    private TextView prompt_button_tv;
    private int upLoadFlag = 0;
    private AlertDialog.Builder builder = null;
    private LinearLayout login = null;
    private Dialog dialog;
    private Button button_01 = null;
    private List<City> list_city = null;
    private List<City> list_city01 = null;
    private List<City> list_city02 = null;
    private PopupMenu popupMenu = null;
    private Menu menu;;
    private int index = 0;
    private MyAsyncTask asyncTask;
    private boolean istrue = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_icons);
        ButterKnife.bind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_face:
//                popupWindow.showAsDropDown(getTopBar(), 0, (int) -ViewUtil.dip2px(this, 52));

                setHeadPicture_Dialog.show();
                break;
            case R.id.take_phone_tv:
                doTakePhoto();
                setHeadPicture_Dialog.dismiss();
                break;
            case R.id.select_picture_tv:
                pickPhoto();
                setHeadPicture_Dialog.dismiss();
                break;
            case R.id.cancel_tv:
                setHeadPicture_Dialog.dismiss();
                break;
            case R.id.prompt_dialog_button:
                picturePrompt_Dialog.dismiss();
                if(upLoadFlag == 1)
                {
                    headPicPost();
                }
                break;


        }
    }
    private void doTakePhoto() {
        //执行拍照前，应该先判断SD卡是否存在
        String SDState = Environment.getExternalStorageState();
        if (SDState.equals(Environment.MEDIA_MOUNTED)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //调用系统相机
            startActivityForResult(intent, PHOTO_WITH_CAMERA);  //用户点击了从相机获取
        } else {
            Toast.makeText(this, "没有SD卡", Toast.LENGTH_LONG).show();
        }
    }
    private void pickPhoto() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_PICK);
        startActivityForResult(intent, PHOTO_WITH_DATA);
    }
    private void headPicPost()
    {
        String uploadURL = "http://121.42.148.205:89/api/headset_up?";
        UploadUtil uploadUtil = UploadUtil.getInstance();
        uploadUtil.setOnUploadProcessListener(this); //设置监听器监听上传状态
        Map<String, String> params = new HashMap<String, String>();
        params.put("uid", MainActivity.myLogin.getMemberid());
        uploadUtil.uploadFile(myPhotoPath,uploadURL,params);
    }
    @Override
    public void onUploadDone(int responseCode, String message) {

    }

    @Override
    public void onUploadProcess(int uploadSize) {

    }

    @Override
    public void initUpload(int fileSize) {

    }
}

